package com.xxxt.utils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.util.CollectionUtils;

/**
 * 切割map和list方法类
 * 
 * @ClassName CollectionUtil
 * @Author MoPeiwen
 * @Date 2020年3月10日 上午4:10:48
 */
public class CollectionUtil {

	private CollectionUtil() {

	}

	/**
	 * 将map切成段，作用与PHP的array_chunk函数相当
	 *
	 * @param chunkMap
	 *            被切段的map
	 * @param chunkNum
	 *            每段的大小
	 * @param <k>
	 *            map的key类型
	 * @param <v>
	 *            map的value类型 如果是自定义类型，则必须实现equals和hashCode方法
	 * @return
	 */
	public static <k, v> List<Map<k, v>> mapChunk(Map<k, v> chunkMap, int chunkNum) {
		if (chunkMap == null || chunkNum <= 0) {
			List<Map<k, v>> list = new ArrayList<>();
			list.add(chunkMap);
			return list;
		}
		Set<k> keySet = chunkMap.keySet();
		Iterator<k> iterator = keySet.iterator();
		int i = 1;
		List<Map<k, v>> total = new ArrayList<>();
		Map<k, v> tem = new HashMap<>();
		while (iterator.hasNext()) {
			k next = iterator.next();
			tem.put(next, chunkMap.get(next));
			if (i == chunkNum) {
				total.add(tem);
				tem = new HashMap<>();
				i = 0;
			}
			i++;
		}
		if (!CollectionUtils.isEmpty(tem)) {
			total.add(tem);
		}
		return total;
	}

	/**
	 * 将list切割
	 *
	 * @param chunkList
	 *            被分隔的数组
	 * @param chunkNum
	 *            每段的大小
	 * @param <T>
	 *            List中的类型
	 * @return
	 */
	public static <T> List<List<T>> listChunk(List<T> chunkList, int chunkNum) {
		if (chunkList == null || chunkNum <= 0) {
			List<List<T>> t = new ArrayList<>();
			t.add(chunkList);
			return t;
		}
		Iterator<T> iterator = chunkList.iterator();
		int i = 1;
		List<List<T>> total = new ArrayList<>();
		List<T> tem = new ArrayList<>();
		while (iterator.hasNext()) {
			T next = iterator.next();
			tem.add(next);
			if (i == chunkNum) {
				total.add(tem);
				tem = new ArrayList<>();
				i = 0;
			}
			i++;
		}
		if (!CollectionUtils.isEmpty(tem)) {
			total.add(tem);
		}
		return total;
	}

}